package com.hoshiibuy.sku.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.sku.entity.AllocationSku;
import com.hoshiibuy.sku.entity.param.AllocationSkuAddParam;
import com.hoshiibuy.sku.entity.param.AllocationSkuQueryParam;
import com.hoshiibuy.sku.entity.param.AllocationSkuUpdateParam;
import com.hoshiibuy.sku.service.AllocationSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * SKU分配 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-05-05
 */
@Slf4j
@RestController
@RequestMapping("/sku/allocation-sku")
@Api(value = "SKU管理", tags = {"SKU分配管理"})
public class AllocationSkuController extends BaseController{
    @Autowired
    private AllocationSkuService allocationSkuService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询SKU分配
    */
    @ApiOperation(value = "分页查询SKU分配")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid AllocationSkuQueryParam param) {
        try {
            log.info("分页查询SKU分配");
            QueryWrapper<AllocationSku> queryWrapper = new QueryWrapper<AllocationSku>();
            queryWrapper.eq(param.getPlatformid() != null, "platformid", param.getPlatformid());
            queryWrapper.eq(param.getStoreid() != null, "storeid", param.getStoreid());
            queryWrapper.eq(param.getVendorid() != null, "vendorid", param.getVendorid());
            queryWrapper.eq(param.getSpuid() != null, "spuid", param.getSpuid());
            queryWrapper.eq(param.getSkuid() != null, "skuid", param.getSkuid());
            queryWrapper.eq(param.getQuantity() != null, "quantity", param.getQuantity());
            queryWrapper.eq(param.getDeleted() != null, "deleted", param.getDeleted());
            IPage<AllocationSku> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(AllocationSku.class));
            return Response.data(allocationSkuService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询SKU分配异常", e);
            return Response.failed("分页查询SKU分配异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询SKU分配
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询SKU分配")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(allocationSkuService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增SKU分配")
    public Response save(@RequestBody @Valid AllocationSkuAddParam param, BindingResult result) {
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                Map<String,Object> errorMap=handleValidMsg(result);
                return Response.failed(JSON.toJSONString(errorMap));
            } else {// 验证通过
                AllocationSku data = BeanUtils.covertType(param, AllocationSku.class);
                return Response.data(allocationSkuService.save(data));
            }
        } catch (ServiceException e) {
            log.error("新增SKU分配异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("新增SKU分配异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改SKU分配
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改SKU分配")
    public Response update(@RequestBody @Valid AllocationSkuUpdateParam param,BindingResult result) {
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                Map<String,Object> errorMap=handleValidMsg(result);
                return Response.failed(JSON.toJSONString(errorMap));
            } else {// 验证通过
                AllocationSku data = BeanUtils.covertType(param, AllocationSku.class);
                return Response.data(allocationSkuService.updateById(data));
            }
        }catch (ServiceException e) {
            log.error("修改SKU分配异常", e);
            return Response.failed(e.getError().getMsg());
        }catch (Exception e) {
            log.error("修改SKU分配异常", e);
            return Response.failed();
        }
    }
}
